//
//  NetworkTools.swift
//  test
//
//  Created by 个人电脑 on 16/3/19.
//  Copyright © 2016年 个人电脑. All rights reserved.
//

import UIKit
import AFNetworking

enum MethodType: String{

    case GET = "GET"
    case POST = "POST"
}

class NetworkTools: AFHTTPSessionManager {

    //给闭包起别名
    typealias Finished = (result: [String : AnyObject]?, error: NSError?)->()
    // 将工具类设计成单例对象
//    static let shareIntance : NetworkTools = NetworkTools()  //错误信息"Request failed: unacceptable content-type: text/plain"
    
    static let shareIntance : NetworkTools = {
    let tools = NetworkTools()
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    }()
}


extension NetworkTools{
    
    func request(methodType: MethodType,URLString: String,parameters: [String: AnyObject], finished :(result: AnyObject?, error: NSError?)->()){
        // 1.定义成功的回调
        let successCallBack = { (task: NSURLSessionDataTask, result: AnyObject?) -> Void in
            finished(result: result, error: nil)
        }
        // 2.定义失败的回调
        let failureCallBAck = { (task: NSURLSessionDataTask?, error: NSError) -> Void in
            finished(result: nil, error: error)
        }
        // 3.根据请求方式发送请求
        if methodType == .GET{
            GET(URLString, parameters: parameters, progress: nil, success: successCallBack, failure: failureCallBAck)
        }else{
            POST(URLString, parameters: parameters, progress: nil, success: successCallBack, failure: failureCallBAck)
        }
    }
}
extension NetworkTools{

//    func loadAccessToken(codeString: String){

//    func loadAccessToken(codeString: String, finished :(result: [String : AnyObject]?, error: NSError?)->()){
   func loadAccessToken(codeString: String, finished :Finished){
       //获取请求URL对应的字符串
        let urlString = "https://api.weibo.com/oauth2/access_token"
        //获取请求参数
        let parameters = ["client_id": app_key,"client_secret": app_secret,"grant_type": "authorization_code", "code" : codeString, "redirect_uri": redirect_uri]
        //发送请求
        
    request(.POST, URLString: urlString, parameters: parameters) { (result, error) -> () in
        finished(result: result as? [String : AnyObject], error: error)
     
        }
    }
}
extension NetworkTools{

//    func loadUserInfo(accessToken: String,uid: String,finished :(result: [String : AnyObject]?, error: NSError?)->()){
    func loadUserInfo(accessToken: String,uid: String,finished :Finished){
        let urlString = "https://api.weibo.com/2/users/show.json"
        let parameters = ["access_token": accessToken, "uid" :uid]
        request(.GET, URLString: urlString, parameters: parameters) { (result, error) -> () in
            finished(result: result as? [String : AnyObject], error: error)

        }
        
    }
}
extension NetworkTools{

    func loadStatuses(since_id : Int,max_id : Int, finished :(result: [[String : AnyObject]]?, error: NSError?)->() ){
    let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        guard let access_token = UserAccountViewModel.shareIntance.account?.access_token else{
        return
        }
        let parameters : [String : AnyObject] = ["access_token" : access_token,"since_id" : since_id,"max_id" : max_id]
        request(.GET, URLString: urlString, parameters: parameters) { (result, error) -> () in
            guard let resultDict = result as? [String : AnyObject] else {
                finished(result: nil, error: error)
            return
            }
            finished(result: resultDict["statuses"] as? [[String :AnyObject]], error: error)
        }
    }
}